//Serial Port Added //1st edit on Neil's Code // hello.stepper.bipolar.44.full.c // // bipolar full stepping hello-world // // Neil Gershenfeld // 11/21/12 // // (c) Massachusetts Institute of Technology 2012 // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose. Copyright is // retained and must be preserved. The work is provided // as is; no warranty is provided, and users accept all // liability. // #include //this library have the commands used with AVR #include // this library used in delay commands #define output(directions,pin) (directions |= pin) // set port direction for output #define set(port,pin) (port |= pin) // set port pin (value =1) #define clear(port,pin) (port &= (~pin)) // clear port pin (value =0) #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define bridge_port PORTA // H-bridge port #define bridge_direction DDRA // H-bridge direction #define Ab (1 << PA0) // H-bridge output pins (setting them to be output) #define Aa (1 << PA1) // " #define Bb (1 << PA3) // " #define Ba (1 << PA4) // " #define on_delay() _delay_us(25) // PWM on time = 25 microseconds #define off_delay() _delay_us(5) // PWM off time = 5 microseconds #define PWM_count 100 // number of PWM cycles #define step_count 20 // number of steps static uint8_t count; //making a variable called "count" // A+ B+ PWM pulse // void pulse_ApBp() { clear(bridge_port, Ab); //disables Ab clear(bridge_port, Bb); //disables Bb set(bridge_port, Aa); //enables Aa set(bridge_port, Ba); //enables Ba for (count = 0; count < PWM_count; ++count) { set(bridge_port, Aa); //enables Aa set(bridge_port, Ba); //enables Ba on_delay(); //PWM on time (Previously defined) clear(bridge_port, Aa); //disables Aa in the end of the pulse clear(bridge_port, Ba); //disables Ba in the end of the pulse off_delay(); //PWM off time (previously defined) } } // // A+ B- PWM pulse // void pulse_ApBm() { clear(bridge_port, Ab); //disables Ab clear(bridge_port, Ba); //disables Ba set(bridge_port, Aa); set(bridge_port, Bb); for (count = 0; count < PWM_count; ++count) { set(bridge_port, Aa); set(bridge_port, Bb); on_delay(); clear(bridge_port, Aa); //disables Aa clear(bridge_port, Bb); //disables Aa off_delay(); } } // // A- B+ PWM pulse // void pulse_AmBp() { clear(bridge_port, Aa); //disables Aa clear(bridge_port, Bb); //disables Aa set(bridge_port, Ab); //enables Ab set(bridge_port, Ba); //enables Ba for (count = 0; count < PWM_count; ++count) { set(bridge_port, Ab); set(bridge_port, Ba); on_delay(); clear(bridge_port, Ab); //disables Aa in the end of the pulse clear(bridge_port, Ba); //disables Ba in the end of the pulse off_delay(); } } // // A- B- PWM pulse // void pulse_AmBm() { clear(bridge_port, Aa); //disables Aa clear(bridge_port, Ba); //disables Ba set(bridge_port, Ab); //enables Ab set(bridge_port, Bb); //enables Bb for (count = 0; count < PWM_count; ++count) { set(bridge_port, Ab); set(bridge_port, Bb); on_delay(); clear(bridge_port, Ab); //disables Aa in the end of the pulse clear(bridge_port, Bb); //disables Aa in the end of the pulse off_delay(); } } // // clockwise step // by this sequence of pulses we will get a step in clockwise direction void step_cw() { pulse_ApBp(); pulse_AmBp(); pulse_AmBm(); pulse_ApBm(); } // // counter-clockwise step // by this sequence of pulses we will get a step in anti clockwise direction void step_ccw() { pulse_ApBm(); pulse_AmBm(); pulse_AmBp(); pulse_ApBp(); } int main(void) { // // main //defining variables static uint8_t i,j; // // set clock divider to /1 // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // // initialize bridge pins (by disabling all the H-bridge ports and setting their values = 0 ) // clear(bridge_port, Aa); output(bridge_direction, Aa); clear(bridge_port, Ab); output(bridge_direction, Ab); clear(bridge_port, Ba); output(bridge_direction, Ba); clear(bridge_port, Bb); output(bridge_direction, Bb); // // main loop // while (1) { for (i = 0; i < step_count; ++i) { for (j = 0; j < i; ++j) step_cw(); for (j = 0; j < i; ++j) step_ccw(); } for (i = step_count; i > 0; --i) { for (j = 0; j < i; ++j) step_cw(); for (j = 0; j < i; ++j) step_ccw(); } } }